package com.my.java.concurrent.reentrantlock;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class TestReenTrantLock2 {

        public static void main(String[] args) {
                final Lock lock = new ReentrantLock();
                // 线程池
                ExecutorService exec = Executors.newCachedThreadPool();

                final Condition cond1 = lock.newCondition();

                Runnable run1 = new Runnable() {
                        @Override
                        public void run() {
                                // 需要先获得锁
                                lock.lock();
                                try {
                                        Thread.sleep(5000);
                                }
                                catch (InterruptedException e) {
                                        // TODO Auto-generated catch block
                                        e.printStackTrace();
                                }
                                try {
                                        //cond1.await();
                                        System.out.println("run1...........");
                                }
//                                catch (InterruptedException e) {
//                                        e.printStackTrace();
//                                }
                                finally {
                                        lock.unlock();
                                }
                        }
                };
                Runnable run2 = new Runnable() {
                        @Override
                        public void run() {
                                // 需要先获得锁
                                try {
                                        Thread.sleep(1000);
                                }
                                catch (InterruptedException e) {
                                        // TODO Auto-generated catch block
                                        e.printStackTrace();
                                }
                                lock.lock();
                                try {
                                        System.out.println("run2...........");
                                        //cond1.signal();
                                }
                                finally {
                                        lock.unlock();
                                }
                        }
                };

                exec.execute(run1);
                exec.execute(run2);
                // 退出线程池
                exec.shutdown();
        }
}
